package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.*;
import com.hzhc.mapper.*;
import com.hzhc.service.SysAuthService;
import com.hzhc.service.YwOrderCarService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.SysAuthVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车辆预约-预约授权 服务实现类
 * </p>
 *
 * @author 1
 * @since 2024-09-29
 */
@Service
@RequiredArgsConstructor
public class SysAuthServiceImpl extends ServiceImpl<SysAuthMapper, SysAuth> implements SysAuthService {

    private final CompanyInfoMapper companyInfoMapper;
    private final CarTeamPlanMapper carTeamPlanMapper;
    private final YwOrderCarService ywOrderCarService;
    private final YwOrderCarMapper ywOrderCarMapper;
    private final YwOrderCntrMapper ywOrderCntrMapper;

    @Override
    public ResultUtil<T> addYwAuth(HttpServletRequest request, SysAuthVo vo) {
        if (vo.getOrderNo().indexOf("TZ") != -1) {
            //提重只能选一个 判断箱子是否理货 理货后无法修改车队
            List<YwOrderCntr> cntrList = ywOrderCntrMapper.selectList(new QueryWrapper<YwOrderCntr>().eq("disable", 0)
                    .eq("order_no", vo.getOrderNo()).ne("tally_state", 0));
            if (cntrList.size() > 0) {
                List<SysAuth> authToCom = baseMapper.getAuthToCom(vo);
                if (authToCom.size() > 0) {
                    //删除已选车队
                    if (vo.getComIds() == null || "".equals(vo.getComIds())) {
                        return ResultUtil.build(400, "部分理货,无法更改车队!");
                    }
                    //更改已选车队
                    if (authToCom.get(0).getSlaveComId() != Integer.parseInt(vo.getComIds())) {
                        return ResultUtil.build(400, "部分理货,无法更改车队!");
                    }
                }
            }
        }
        String comIds = vo.getComIds();
        //先清除当前业务所授权的车队
        LambdaQueryWrapper<SysAuth> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysAuth::getOrderNo, vo.getOrderNo());
        baseMapper.delete(wrapper);

        List<YwOrderCar> delYwOrders = ywOrderCarMapper.selectList(new QueryWrapper<YwOrderCar>().eq("disable", 0).eq("order_no", vo.getOrderNo()));
        if(delYwOrders.size()>0){
            for (YwOrderCar delYwOrder : delYwOrders) {
                delYwOrder.setDisable(1);
            }
            ywOrderCarService.updateBatchById(delYwOrders);
        }

        if (comIds == null || "".equals(comIds)) {
            return ResultUtil.build(200, "保存成功！");
        }

        //再新增授权车队

        String[] split = comIds.split(",");
        List<SysAuth> list = new ArrayList<>();
        for (String comId : split) {
            SysAuth auth = new SysAuth();
            auth.setOrderNo(vo.getOrderNo());
            auth.setSlaveComId(Integer.parseInt(comId));
            auth.setOrderType(vo.getOrderNo().substring(0, 2));
            list.add(auth);
        }
        boolean b = saveBatch(list);
        if (b) {
            //给 yw_order_car 添加数据
            if (vo.getOrderNo().indexOf("SGJ") == -1 ) {
                CompanyInfo companyInfo = companyInfoMapper.selectById(vo.getComIds());
                if (companyInfo != null) {
                    String orderType = vo.getOrderNo().substring(0, 2);
                    if("KJ".equals(orderType)){
                        orderType="ZJ";
                    }
                    CarTeamPlan carTeamPlan = carTeamPlanMapper.selectOne(new QueryWrapper<CarTeamPlan>().eq("disable", 0).eq("company_id", companyInfo.getId())
                            .eq("order_type",orderType));
                    if (carTeamPlan != null) {
                        List<YwOrderCar> ywOrderCars = ywOrderCarMapper.selectList(new QueryWrapper<YwOrderCar>().eq("disable", 0).eq("order_no", vo.getOrderNo())
                                .like("order_no",vo.getOrderNo().substring(0, 2)));
                        if (ywOrderCars.size() > 0) {
                            for (YwOrderCar ywOrderCar : ywOrderCars) {
                                ywOrderCar.setDisable(1);
                            }
                            ywOrderCarService.updateBatchById(ywOrderCars);
                        }
                        String[] split1 = carTeamPlan.getCarNo().split(",");
                        List<YwOrderCar> saveList = new ArrayList<>();
                        for (String s : split1) {
                            YwOrderCar ywOrderCar = new YwOrderCar();
                            ywOrderCar.setOrderNo(vo.getOrderNo());
                            ywOrderCar.setTruckNo(s);
                            ywOrderCar.setCompanyId(companyInfo.getId());
                            ywOrderCar.setCompanyName(companyInfo.getName());
                            saveList.add(ywOrderCar);
                        }
                        ywOrderCarService.saveBatch(saveList);
                    }
                }
            }
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public List<SysAuth> getAuthToCom(SysAuthVo vo) {
        return baseMapper.getAuthToCom(vo);
    }

    @Override
    public List<String> selectByAuth(Long companyId, String orderType) {
        return baseMapper.selectByAuth(companyId, orderType);
    }
}
